<?php
require("global.php");
if(isset($phprpg->input['buy'])){
	$getItem = $db->select("items","*","id=".intval($phprpg->input['buy']));
	if($db->recordcount($getItem) == 1){
		$i = $db->fetchrow($getItem);
		if($phprpg->player['money'] < $i['price']){
			$error[] = "You don't have enough money.";
		}
		if(!isset($error)){
			$update['money'] = $phprpg->player['money'] - $i['price'];
			$db->update("players",$update,"pid=".$phprpg->player['pid']);

			$insert['pid'] = $phprpg->player['pid'];
			$insert['iid'] = $i['id'];
			$db->insert("inventory",$insert);
			echo $db->query;
			header("location: inventory.php");
		}
	}else{
		$error[] = "This item doesn't exist.";
	}
	if(isset($error)){
		$errors = $phprpg->inline_error($error);
	}
}
if(isset($phprpg->input['search'])){
	$conditionals = array();
	if($phprpg->input['itemname'] != ""){
		$conditionals[] = "`name` LIKE '%".$db->escape($phprpg->input['itemname'])."%'";
	}
	if($phprpg->input['type'] != ""){
		 $conditionals[] = "`type`='".$db->escape($phprpg->input['type'])."'";
	}
	$where = implode(" AND ",$conditionals);
	if($phprpg->input['type'] == "weapon"){
		$weapon = " selected";
	}
	if($phprpg->input['type'] == "armor"){
		$armor = " selected";
	}
	if($phprpg->input['type'] == ""){
		$all = " selected";
	}
}else{
	$where = "";
}
$getItems = $db->select("items","*",$where);
while($i = $db->fetchrow($getItems)){
	eval("\$items .= \"".$templates->get("shops_item")."\";");
}
eval("\$shop = \"".$templates->get("shops")."\";");
$templates->output($shop);